App.Modules.MailWizard = function(obj){
	var data = obj.dataObj.data;
	var localData = new Object();
	var el = obj.elm_moduleContent;
	localData.isLogged = false;
	var idLocalized = 0;

    obj.setOptions({confirmOnClose: false});

	var availableEngines = [
		{id:"0", title:"Wanadoo", iconUrl:"img\/wanadoo.png", ico:"ico\/orange.png", parameters :{login:"%{user}", protocol:"/pop3", server:"pop.wanadoo.fr", port:"110", secure:0, webMail:"http://www.wanadoo.fr"}},
		{id:"1", title:"Free", iconUrl:"img\/free.png", ico:"ico\/free.png", parameters :{login:"%{user}", protocol:"/imap", server:"imap.free.fr", port:"143", secure:0, webMail:"http://imp.free.fr"}},
		{id:"2", title:"Neuf", iconUrl:"img\/neuf.png", ico:"ico\/neuf.png", parameters :{login:"%{user}", protocol:"/imap", server:"imap.neuf.fr", port:"143", secure:0, webMail:"http://webmail.neuf.fr"}},
		
		{id:"3", title:"\u65b0\u6d6a", iconUrl:"img\/sinamail.png", ico:"ico\/sina.png", parameters :{login:"%{user}", protocol:"/pop3", server:"pop3.sina.com.cn", port:"110", secure:0, webMail:"http://mail.sina.com.cn"}},
		{id:"4", title:"TOM", iconUrl:"img\/tomemail.png", ico:"ico\/tom.png", parameters :{login:"%{user}", protocol:"/pop3", server:"pop.tom.com", port:"110", secure:0, webMail:"http://mail.top.com"}},
		{id:"5", title:"QQ", iconUrl:"img\/qqmail.png", ico:"ico\/qq.png", parameters :{login:"%{user}", protocol:"/pop3", server:"pop.qq.com", port:"110", secure:0, webMail:"http://mail.qq.com"}},
		
		{id:"6", title:"GMX", iconUrl:"img\/gmxde.png", ico:"ico\/gmxde.png", parameters :{login:"%{user}", protocol:"/pop3", server:"pop.gmx.net", port:"110", secure:0, webMail:"http://mail.gmx.net"}},
		{id:"7", title:"Web.de", iconUrl:"img\/webde.png", ico:"ico\/webde.png", parameters :{login:"%{user}", protocol:"/imap", server:"imap.web.de", port:"143", secure:0, webMail:"http://www.web.de"}},
		{id:"8", title:"T-Online.de", iconUrl:"img\/tonline.png", ico:"ico\/tonline.png", parameters :{login:"%{user}", protocol:"/pop3", server:"popmail.t-online.de", port:"110", secure:0, webMail:"https:\/\/email.t-online.de"}}
		];	
	
	var engines = new Array();
	var enginesId = new Array();
	var showLocales = true;
	
	if(User.locale == "fr" || App.locale =="fr"){ 
		enginesId=[0,1,2]; 
	} else if(User.locale == "de" || App.locale =="de"){
		enginesId=[6,7,8];
	}else if(User.locale == "cn" || App.locale =="cn"){
		enginesId=[3,4,5];
	}else {
		enginesId=[0,1,2];
		showLocales = false;
	}
	
	for (var z=0; z<enginesId.length; z++) {
		for (var i=0; i<availableEngines.length; i++) {
			if(enginesId[z] == availableEngines[i].id)
			engines.push(availableEngines[i]);
		}
	}
	
	function displayWizard() {
		
		el.innerHTML = '';
		var rand = Math.round(Math.random()*1000);
		
		var innerLocalEngines = '';
		
		for(var z=0;z<engines.length;z++){
			innerLocalEngines += '<td style="'+(showLocales ? ((engines[z].id == 8) ? "display:none" : "") : "display:none")+'"><label for="'+engines[z].title+'_'+rand+'"><img src="'+NV_PATH+'modules/mailWizard/'+engines[z].iconUrl+'" /><br/>'+
				'<b>'+engines[z].title+'</b><br /></label><input id="'+engines[z].title+'_'+rand+'" type="radio" name="radio" style="margin-top:5px" value="" /></td>';
		}
		
		var divPage1 = document.createElement("div");
		divPage1.innerHTML = '<div style="color: #F60;font-weight: bold">'+_("Check your email on Netvibes")+'</div><form><div style="margin-top:5px;marginleft:10px"><b>'+_("Step 1 out of 2:")+" "+'</b>'+_("Please select your e-mail provider")+'</div>'+
					'<table width="100%"><tr><td align="center"><table cellpadding="0" cellspacing="0" class="formTable" width="100%">'+
					'<tr align="center">'+
						'<td valign="bottom" width="30%"><label for="gmail_'+rand+'"><img src="'+NV_PATH+'modules/mailWizard/img/gmail.png" /><br/><b>GMail</b><br /></label><input id="gmail_'+rand+'" type="radio" name="radio" style="margin-top:5px" value="GmailInfos" /></td>'+ //width="30" height="30"
						'<td valign="bottom" width="30%"><label for="hotmail_'+rand+'"><img src="'+NV_PATH+'modules/mailWizard/img/hotmail.png" /><br/><b>Hotmail</b><br /></label><input id="hotmail_'+rand+'" type="radio" name="radio" style="margin-top:5px" value="HotmailInfos" /></td>'+ //width="20" height="15"
						'<td valign="bottom" width="30%"><label for="yahoomail_'+rand+'"><img src="'+NV_PATH+'modules/mailWizard/img/yahoo.png" /><br/><b>Yahoo! Mail</b><br /></label><input id="yahoomail_'+rand+'" type="radio" name="radio" style="margin-top:5px" value="YahooMailInfos" /></td>'+					
					'</tr>'+
					'<tr align="center">'+
						'<td><label for="aolmail_'+rand+'"><img src="'+NV_PATH+'modules/mailWizard/img/aol.png" /><br/><b>AOL</b><br /></label><input id="aolmail_'+rand+'" type="radio" name="radio" style="margin-top:5px" value="AolMailInfos" /></td>'+ //width="30" height="30"  
						'<td><label for="macmail_'+rand+'"><img src="'+NV_PATH+'modules/mailWizard/img/apple.png" /><br/><b>.Mac</b><br /></label><input id="macmail_'+rand+'" type="radio" name="radio" style="margin-top:5px" value="MacMailInfos" /></td>'+ //width="20" height="15"
						'<td><label for="popmail_'+rand+'"><img src="'+NV_PATH+'modules/mailWizard/img/pop.png" /><br/><b>'+_("Other (POP)")+'</b><br /></label><input id="popmail_'+rand+'" type="radio" name="radio" style="margin-top:5px" value="PopMailInfos" /></td>'+					
					'</tr>'+
					'<tr align="center">'+innerLocalEngines+'</tr>'+					
				'</table></td></tr></table></form>';
				
		var divPage2 = document.createElement("div");
		divPage2.style.display = "none";
		var textGeneral = '<div>'+_("You have selected")+' <span>%1</span> <a style="color: #F60" href="#">'+_("(change)")+'</a></div>'+ //style="color: #F60;font-weight: bold"
							'<span></span>'+
							'<div style="margin-top:5px;marginleft:10px"><b>'+_("Step 2 out of 2:")+" "+'</b>'+_("Type your login and password for")+'</div>'+
							 //'<img style="position:absolute;margin-top:8px;margin-left:5px;" src="'+NV_PATH+'modules/mailWizard/img/email.jpg" />'+
							'<form><table width="100%"><tr><td align="center"><table cellpadding="0" cellspacing="0" class="formTable" style="width:70%;margin-top:5px">'+
								'<tr>'+
									'<td><label>' + _("Login:") + ' </label></td>'+
									'<td><input type="text" class="inputClean" accesskey="t" maxlength="150" value="" style="width:75px"/></td>'+
								'</tr>'+
								'<tr>'+
									'<td><label>' + _("Password:") + ' </label></td>'+
									'<td><input type="password" class="inputClean" AUTOCOMPLETE="OFF" accesskey="t" maxlength="150" style="width:75px"/></td>'+

								'</tr>'+
								/* Popmail*/
								'<tr>'+
									'<td><label>' + _("Protocol:") + '</label></td>'+
									'<td><select style="width:80px"/><option value="/pop3">POP3</option><option value="/imap">IMAP4</option></select></td>'+
								'</tr>'+
								'<tr>'+
									'<td><label>' + _("Secure:") + '</label></td>'+
									'<td><input type="checkbox" /></td>'+
								'</tr>'+
								'<tr>'+
									'<td><label>' + _("Mail server:") + '</label></td>'+
									'<td><input type="text" class="inputClean" accesskey="t" maxlength="150" style="width:75px"/></td>'+
								'</tr>'+
								'<tr>'+
									'<td><label>' + _("Port:") + '</label></td>'+
									'<td><input type="text" class="inputClean" accesskey="t" maxlength="6" style="width:75px" value="110" /></td>'+
								'</tr>'+
								'<tr>'+
									'<td><label>' + _("Webmail URL:") + '</label></td>'+
									'<td><input type="text" class="inputClean" accesskey="t" maxlength="1024" style="width:75px"/></td>'+
								'</tr>'+
								/*  End Popmail */
								'<tr>'+
									'<td colspan="2" align="center"><input type="submit" value="'+_("Finish")+'" class="buttonClean"/></td>'+
								'</tr>'+								
							'</table></td></tr></table>';

		divPage2.innerHTML = textGeneral;
		
		el.appendChild(divPage1);
		el.appendChild(divPage2);

		var radios = divPage1.getElementsByTagName("input");
		var tds = divPage1.getElementsByTagName("td");
		var inputCreateModule = divPage2.getElementsByTagName("tr")[8].getElementsByTagName("input")[0];
		var login = divPage2.getElementsByTagName("input")[0]; 
		var pass = divPage2.getElementsByTagName("input")[1]; 
		var moduleName = '';
		var arrayPopmail = Array();
		var spanInfos = divPage2.getElementsByTagName("span")[1];
		var explain_link = false;
		
		for(var i=0; i<5; i++){
			arrayPopmail[i] = divPage2.getElementsByTagName("tr")[i+3];
			arrayPopmail[i].style.display = 'none';
		}
		
		if(!obj.readOnly){
			tds[1].firstChild.onclick = function() { radios[0].onclick(); }
			radios[0].onclick = function(){ //gmail
				divPage2.getElementsByTagName("label")[0].innerHTML =  _("Login:") + ' ';
				moduleName = this.value;
				for(var i=0; i<5; i++){
					arrayPopmail[i].style.display = 'none';
				}
				spanInfos.innerHTML = '';
				nextStep(moduleName);
			}
			tds[2].firstChild.onclick = function() { radios[1].onclick(); }
			radios[1].onclick = function(){ //hotmail
				divPage2.getElementsByTagName("label")[0].innerHTML =  _("Email address:") + ' ';
				moduleName = this.value;
				for(var i=0; i<5; i++){
					arrayPopmail[i].style.display = 'none';
				}
				//spanInfos.innerHTML = _("Please note that this module is not compatible with Windows Live accounts.");
				nextStep(moduleName);
			}
			tds[3].firstChild.onclick = function() { radios[2].onclick(); }
			radios[2].onclick = function(){ //yahoomail
				divPage2.getElementsByTagName("label")[0].innerHTML =  _("Login:") + ' ';
				moduleName = this.value;
				nextStep(moduleName);
			}
			tds[4].firstChild.onclick = function() { radios[3].onclick(); }
			radios[3].onclick = function(){ //aol
				divPage2.getElementsByTagName("label")[0].innerHTML =  _("Screen name:") + ' ';
				moduleName = this.value;
				for(var i=0; i<5; i++){
					arrayPopmail[i].style.display = 'none';
				}
				spanInfos.innerHTML = '';
				nextStep(moduleName);
			}
			tds[5].firstChild.onclick = function() { radios[4].onclick(); }
			radios[4].onclick = function(){ //mac
				divPage2.getElementsByTagName("label")[0].innerHTML =  _("Login:") + ' ';
				moduleName = this.value;
				for(var i=0; i<5; i++){
					arrayPopmail[i].style.display = 'none';
				}
				spanInfos.innerHTML = '';
				nextStep(moduleName);
			}
			tds[6].firstChild.onclick = function() { radios[5].onclick(); }
			radios[5].onclick = function(){ //popmail
				divPage2.getElementsByTagName("label")[0].innerHTML =  _("Login:") + ' ';
				moduleName = this.value;
				for(var i=0; i<5; i++){
					if (Browser.isIE) {
						arrayPopmail[i].style.display = 'block';
					}					
					else {
						arrayPopmail[i].style.display = 'table-row';
					}
				}
				spanInfos.innerHTML = '';
				nextStep(moduleName);
			}
			
			tds[7].firstChild.onclick = function() { radios[6].onclick(); }
			radios[6].onclick = function(){ //popmail
				divPage2.getElementsByTagName("label")[0].innerHTML =  _("Login:") + ' ';
				moduleName = 'localized';
				idLocalized = enginesId[0];
				for(var i=0; i<5; i++){
					arrayPopmail[i].style.display = 'none';
				}
				spanInfos.innerHTML = '';
				nextStepLocalized(engines[0].title, engines[0].id);
			}	
			
			tds[8].firstChild.onclick = function() { radios[7].onclick(); }
			radios[7].onclick = function(){ //popmail
				divPage2.getElementsByTagName("label")[0].innerHTML =  _("Login:") + ' ';
				moduleName = 'localized';
				idLocalized = enginesId[1];
				for(var i=0; i<5; i++){
					arrayPopmail[i].style.display = 'none';
				}
				spanInfos.innerHTML = '';
				nextStepLocalized(engines[1].title, engines[1].id);
			}	
			
			tds[9].firstChild.onclick = function() { radios[8].onclick(); }
			radios[8].onclick = function(){ //popmail
				divPage2.getElementsByTagName("label")[0].innerHTML =  _("Login:") + ' ';
				moduleName = 'localized';
				idLocalized = enginesId[2];
				for(var i=0; i<5; i++){
					arrayPopmail[i].style.display = 'none';
				}
				spanInfos.innerHTML = '';
				nextStepLocalized(engines[2].title, engines[2].id);
			}				
		}
		
		var divNext = document.createElement("div");
        divNext.className = "nv-pager";
		divNext.style.paddingTop = "2px";
		
		var next = document.createElement("a");
		next.innerHTML = _("next");
		next.rel = "next";
		next.className = "next";
		next.style.display = "none"

		var previous = document.createElement("a");
		previous.innerHTML = _("Change e-mail provider");
		previous.style.display = "none";
		previous.rel = "prev";
		previous.className = "prev";
		
		var protocol = arrayPopmail[0].getElementsByTagName('select')[0];
		var secure = arrayPopmail[1].getElementsByTagName('input')[0];
		var port = arrayPopmail[3].getElementsByTagName('input')[0];
		
		protocol.onchange = findPort;
		secure.onchange = findPort;
		
		
		function findPort() {
			if (protocol.value == "/pop3") {
				if (secure.checked)
					port.value = 995;
				else
					port.value = 110;
			}
			else {
				if (secure.checked)
					port.value = 993;
				else
					port.value = 143;
			}
		}

		previous.onclick = previousStep;
		
		function previousStep(){
			divPage2.style.display = "none";
			divPage1.style.display = "block";
			previous.style.display = "none";
			obj.updateFavicon(App.getStaticUrl(NV_PATH + "img/wand.png"), false);
			//next.style.display = "block";

			return false;
		}
		
		function nextStep(moduleName){
			divPage2.style.display = "block";
			divPage1.style.display = "none";
			//this.style.display = "none";
			previous.style.display = "block";
			
			// Replace %1 in "you have selected %1
			var mailName = divPage2.getElementsByTagName('div')[0].getElementsByTagName('span')[0];
			mailName.innerHTML = getSexyName(moduleName);
			
			// for the (change) link -> previous
			divPage2.getElementsByTagName('div')[0].getElementsByTagName('a')[0].onclick = previousStep;
			obj.updateFavicon(getFaviconUrl(moduleName), false);

			return false;
		}
		
		function nextStepLocalized(title, id){
			divPage2.style.display = "block";
			divPage1.style.display = "none";
			//this.style.display = "none";
			previous.style.display = "block";
			
			// Replace %1 in "you have selected %1
			var mailName = divPage2.getElementsByTagName('div')[0].getElementsByTagName('span')[0];
			mailName.innerHTML = title;
			
			// for the (change) link -> previous
			divPage2.getElementsByTagName('div')[0].getElementsByTagName('a')[0].onclick = previousStep;
			obj.updateFavicon(engines[id].ico, false);
			
			return false;		
		}

		function getSexyName(moduleName){
			switch(moduleName) {
 				case "GmailInfos": 
					return "GMail";
					break;
				case "HotmailInfos": 
					return "Hotmail";
					break;				
				case "AolMailInfos": 
					return "Aol Mail";
					break;
				case "YahooMailInfos": 
					return "Yahoo! Mail";
					break;
				case "MacMailInfos": 
					return ".Mac";
					break;
				case "PopMailInfos": 
					return "Other (POP)";
					break;
			}
				    
		}

		function getFaviconUrl(moduleName){
			switch(moduleName) {
 				case "GmailInfos": 
					return App.getStaticUrl(NV_PATH + "img/gmail.gif");
					break;
				case "HotmailInfos": 
					return App.getStaticUrl(NV_PATH + "modules/hotmail/img/hotmail.png");
					break;				
				case "AolMailInfos": 
					return App.getStaticUrl(NV_PATH + "img/aol.gif");
					break;
				case "YahooMailInfos": 
					return App.getStaticUrl(NV_PATH + "img/yahoomail.gif");
					break;
				case "MacMailInfos": 
					return App.getStaticUrl(NV_PATH + "modules/macMail/icone_mac.png");
					break;
				case "PopMailInfos": 
					return App.getStaticUrl(NV_PATH + "img/popmail.gif");
					break;
			}
		}

		
		inputCreateModule.onclick = function(){
			
			newData = new Object();
			newData.login = login.value;
			newData.pass = pass.value;
			
			if(moduleName == "PopMailInfos"){
				newData.protocol = arrayPopmail[0].getElementsByTagName('select')[0].value;
				newData.secure = (arrayPopmail[1].getElementsByTagName('input')[0].checked ? 1 : 0 );
				newData.server = arrayPopmail[2].getElementsByTagName('input')[0].value;
				newData.port = arrayPopmail[3].getElementsByTagName('input')[0].value;
				newData.webmail = arrayPopmail[4].getElementsByTagName('input')[0].value;
			}else if(moduleName == "localized"){
				newData.protocol = availableEngines[idLocalized].parameters.protocol;
				newData.secure = availableEngines[idLocalized].parameters.secure;
				newData.server = availableEngines[idLocalized].parameters.server;
				newData.port = availableEngines[idLocalized].parameters.port;
				newData.webmail = availableEngines[idLocalized].parameters.webMail;	
				newData.ico = availableEngines[idLocalized].ico;
				newData.iconUrl = availableEngines[idLocalized].iconUrl;
				newData.provider = availableEngines[idLocalized].title;
			}
			addModule(moduleName, newData);
			el.innerHTML = '';
			return false;
		}
		
		divNext.appendChild(previous);	
		divNext.appendChild(next);
		
		el.appendChild(divNext)
		
        return false;
	}



	function addModule(moduleName, newData){
		wasLocalized = false;
		if(moduleName == 'localized'){
			moduleName = 'PopMailInfos';
			wasLocalized = true;
		}
		
		var moduleDataObj = App.Modules[moduleName].defaultObj;		
				
		var dataObj = new Object();
		for (var i in moduleDataObj) dataObj[i] = moduleDataObj[i];
		dataObj.data = new Object();
		for (var i in moduleDataObj.data) dataObj.data[i] = moduleDataObj.data[i];
									
		dataObj.data = new Object();
		dataObj.data.login = newData.login;
		dataObj.data.pass = newData.pass;
		
		if(moduleName == "PopMailInfos"){
			dataObj.data.protocol = newData.protocol;
			dataObj.data.secure = newData.secure;
			dataObj.data.server = newData.server;
			dataObj.data.port = newData.port;
			dataObj.data.webmail = newData.webmail;
		}
		if(wasLocalized){
			dataObj.data.ico = NV_PATH+'modules/mailWizard/'+newData.ico;
			dataObj.data.iconUrl = NV_PATH+'modules/mailWizard/'+newData.iconUrl;
			dataObj.data.provider = newData.provider;
		}
		
		dataObj.container = obj.dataObj.container;
		dataObj.tab = App.selectedTab;


		var mod = new Module(dataObj, {preview: obj.previewMode ? true : false});

		if (!obj.previewMode) { // LX
			mod.load();
			App.moduleList.push(mod);
			App.ColManager["col"+obj.dataObj.container].insertBefore(mod.elm_module, obj.elm_module); 
			App.saveUserData('new', mod);
		} else {
			// in preview mode, explicitly inject module in its container
			mod.inject(obj.elm_module.parentNode).load();
		}

		// LX: store real widget instance to div.module HTMLElement (#2417)
		obj.elm_module.store('widget', mod);

		obj.elm_module.style.display = "none";
		
		closeWizard = function() {
			if (mod.dataObj.id) {
				obj.remove(true);
				window.clearInterval(closeInterval);
			}
			return false;
		}
		
		var closeInterval = window.setInterval("closeWizard()",500);
		return false;
	}
	
	displayWizard();

}
